<?php
class Blogadmin extends CI_Controller{
	
	function __construct(){
		parent::__construct();
		$this->is_logged_in();
	}
	function index(){
		$this->load->model('category_model');
		$data['records'] = $this->category_model->get_valid_category();
		$data['main_content'] = 'blogadmin_view';
		$this->load->view('includes/blogtemplate', $data);
	
	}
	
	function is_logged_in(){
		$is_logged_in = $this->session->userdata('is_logged_in');
		if(!isset($is_logged_in) || $is_logged_in != true){
			redirect('login');
		}
	}
	
	function categories(){
		$this->load->model('category_model');
		$data['records'] = $this->category_model->get_valid_category();
		$data['main_content'] = 'blogadmin_view';
		$this->load->view('includes/blogtemplate', $data);
	}
	
	function addcategory(){
		$this->load->model('category_model');
		$data['records'] = $this->category_model->get_valid_category();
		$data['main_content'] = 'category_add_new_view';
		$this->load->view('includes/blogtemplate', $data);
	}
	function createnewcategory(){
		$data = array(
			'category_name' => $this->input->post('category_name'),
			'category_description' => $this->input->post('category_description'),
			'category_basename' => $this->input->post('category_basename'),
			'category_parent' => $this->input->post('category_parent'),
		);
		
		$this->load->model('category_model');
		if($this->category_model->create_category($data) == true){
			redirect('blogadmin');
		}
	}
	function deletecategory(){
		$this->load->model('category_model');
		$this->category_model->delete_category();
		redirect('blogadmin');
	}
	
	function updatecategory(){
		$this->load->model('category_model');
		$id['id'] = $this->uri->segment(3);
		$data['records'] = $this->category_model->get_not_like_category($id);
		$data['record'] = $this->category_model->get_category();
		$data['main_content'] = 'category_update_view';
		$this->load->view('includes/blogtemplate', $data);
	}
	
	function update_category(){
		$data = array(
			'category_id' => $this->input->post('category_id'),
			'category_name' => $this->input->post('category_name'),
			'category_basename' => $this->input->post('category_basename'),
			'category_parent' => $this->input->post('category_parent'),
			'category_description' => $this->input->post('category_description'),
		);
		
		$this->load->model('category_model');
		if($this->category_model->update_category($data) == true){
			redirect('blogadmin');
		}
		
		
	
	}
	
	function entries(){
		$this->load->model('entry_model');
		$data['records'] = $this->entry_model->get_entries();
		$data['main_content'] = 'entries_view';
		$this->load->view('includes/blogtemplate', $data);
		
	}
	
	function addentry(){
		$this->load->model('category_model');
		$data['records'] = $this->category_model->get_valid_category();
		$data['main_content'] = 'entry_add_new_view';
		$this->load->view('includes/blogtemplate', $data);
	
	}
	
	function add_new_entry(){
		$data = array(
			'entry_name' => $this->input->post('entry_name'),
			'entry_basename' => $this->input->post('entry_basename'),
			'entry_content' => $this->input->post('entry_content'),
			'entry_category_parent' => $this->input->post('entry_category_parent'),
			'entry_author' => $this->input->post('entry_author'),
			'entry_date' => $this->input->post('entry_date'),
			'entry_pubish' => 0,
		);
		
		$this->load->model('entry_model');
		if($this->entry_model->add_new_entry($data) == true){
			redirect('blogadmin/entries');
		}
	
	}
	
	function delete_entry(){
		$this->load->model('entry_model');
		if($this->entry_model->delete_entry() == true){
			redirect('blogadmin/entries');
		}
	}
	
	function updateentry(){
		$this->load->model('category_model');
		$data['records'] = $this->category_model->get_valid_category();
		$this->load->model('entry_model');
		$data['record'] = $this->entry_model->get_entry();
		$data['main_content'] = 'update_entry_view';
		$this->load->view('includes/blogtemplate', $data);
		
	}
	
	function update_entry(){
		$data = array(
			'entry_id' => $this->input->post('entry_id'),
			'entry_name' => $this->input->post('entry_name'),
			'entry_basename' => $this->input->post('entry_basename'),
			'entry_content' => $this->input->post('entry_content'),
			'entry_category_parent' => $this->input->post('entry_category_parent'),
			'entry_author' => $this->input->post('entry_author'),
			'entry_date' => $this->input->post('entry_date'),
		);
		
		$this->load->model('entry_model');
		if($this->entry_model->update_entry($data) == true){
			redirect('blogadmin/entries');
		}
	
	}
	
	function publish(){
		$this->load->model('entry_model');
		$data['records'] = $this->entry_model->get_entry_unpublish();
		$data['main_content'] = 'entry_publish';
		$this->load->view('includes/blogtemplate', $data);
	
	}
	function unpublish(){
		$this->load->model('entry_model');
		$data['records'] = $this->entry_model->get_entry_publish();
		$data['main_content'] = 'entry_unpublish_view';
		$this->load->view('includes/blogtemplate', $data);
	
	}
	
	function publishentry(){
		$this->load->model('entry_model');
		if($this->entry_model->publish_entry() == true){
			redirect('blogadmin/publish');
		}
		
	}
	function unpublishentry(){
		$this->load->model('entry_model');
		if($this->entry_model->unpublish_entry() == true){
			redirect('blogadmin/unpublish');
		}
		
	}

}
